extern crate fejpsl_contract;
use fejpsl_contract::wall::Wall;
use self::fejpsl_contract::wall::{WallComment, WallReader, WallListData, WallPageData};

pub async fn get_wall_list_by_page_no(page_no:&i32,user_uuid:&String) ->WallPageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_wall = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset=*page_no as i64 *per_page_num;
    let rows = &client.query("select a.id,a.user_uuid,a.exam_subject_id,a.create_time,a.message,a.read_num,a.comment_num,b.avatar_text,
    b.nice_name,b.real_name,b.is_explicit,c.name from (select * from t_wall  where user_uuid in (SELECT follow_uuid
	FROM public.t_follow_me where user_uuid=$3) or user_uuid=$3
	order by create_time desc limit 100) a left join t_user b on a.user_uuid=b.user_uuid
	left join t_exam_paper c on a.exam_subject_id=c.subject_id order by create_time desc limit $1 offset $2",
                             &[&per_page_num,&offset,&user_uuid]).await.unwrap();
    for row in rows {
        let mut m_message=String::from("");
        let tmp_str: Option<String> = row.get(4);
        if let Some(_i) = tmp_str {m_message= row.get(4); }else{ m_message=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(7);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(7); }else{ m_avatar_text=String::from("") }
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(9);
        if let Some(_i) = tmp_str {m_real_name= row.get(9); }else{ m_real_name=String::from("") }
        let wall = Wall {
            id:                 row.get(0),
            user_uuid:          row.get(1),
            exam_subject_id:    row.get(2),
            create_time:        row.get(3),
            message:            m_message,
            read_num:           row.get(5),
            comment_num:        row.get(6),
            avatar_text:        m_avatar_text,
            nice_name:          row.get(8),
            real_name:          m_real_name,
            is_explicit:        row.get(10),
            name:               row.get(11),
        };

        vec_wall.push(wall);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let wall_list_data= WallListData {
        cur_page:*page_no,
        datas:Some(vec_wall),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let wall_page_data= WallPageData {
        data:wall_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return wall_page_data;

}

pub async fn get_wall_reader_list_by_wall_id(wall_id:&i32) -> Option<Vec<WallReader>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_wall_reader = Vec::new();
    let rows = &client.query("select a.id,a.wall_id,a.user_uuid,b.avatar_text from (select id,wall_id,user_uuid
    from public.t_wall_reader where wall_id=$1) a
    left join t_user b on a.user_uuid=b.user_uuid order by id desc limit 30",
                             &[&wall_id]).await.unwrap();
    for row in rows {
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(3);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(3); }else{ m_avatar_text=String::from("") }
        let wall_reader = WallReader {
            id:                 row.get(0),
            wall_id:          row.get(1),
            user_uuid:    row.get(2),
            avatar_text:        m_avatar_text,
        };

        vec_wall_reader.push(wall_reader);
    }
    return Some(vec_wall_reader);
}

pub async fn get_wall_comment_list_by_page_no(page_no:&i32,wall_id:&i32) -> Option<Vec<WallComment>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_wall_comment = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset=*page_no as i64*per_page_num;
    let rows = &client.query("select a.id,a.wall_id,a.user_uuid,a.comment,a.create_time,b.nice_name,b.real_name,b.is_explicit
    from (select * from public.t_wall_comment where wall_id=$3) a
    left join t_user b on a.user_uuid=b.user_uuid order by create_time desc limit $1 offset $2",
                             &[&per_page_num,&offset,&wall_id]).await.unwrap();
    for row in rows {
        let mut m_nice_name=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_nice_name= row.get(5); }else{ m_nice_name=String::from("") }
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(6);
        if let Some(_i) = tmp_str {m_real_name= row.get(6); }else{ m_real_name=String::from("") }
        let wall_comment = WallComment {
            id:                 row.get(0),
            wall_id:          row.get(1),
            user_uuid:    row.get(2),
            comment:        row.get(3),
            create_time:            row.get(4),
            nice_name:          m_nice_name,
            real_name:          m_real_name,
            is_explicit:        row.get(7),
        };

        vec_wall_comment.push(wall_comment);
    }
    return Some(vec_wall_comment);
}
